package com.ita.ftp;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class FtpServer implements Runnable{
	
	private Socket socket;
	
	public final static String PATH = "./files";

	public FtpServer(Socket socket) {
		super();
		this.socket = socket;
	}
	
	boolean checkCommand(OutputStream outputStream, String commandStr) throws IOException{
		String[] command = commandStr.split(" ");
		if(command.length != 2){
			outputStream.write(("-- Your command " + command[0] + " error. Pls input again.\n").getBytes());
			System.out.println("out init");
			return false;
		}else {
			return true;
		}
	}
	
	public void getFileList(OutputStream outputStream) throws IOException {
		outputStream.write("-- Get file list!\n".getBytes());
		File file = new File(PATH);
//		System.out.println(file.isDirectory());
		String [] fileList = file.list();
		StringBuffer fileListStr = new StringBuffer();
		for (String string : fileList) {
			fileListStr.append(string + "\n");
		}
		outputStream.write(fileListStr.toString().getBytes());
	}
	
	public void downloadFile(String fileName, OutputStream outputStream) throws IOException {
		outputStream.write("-- Download file!\n".getBytes());
		String filePath = PATH + "/" + fileName;
		try {
			InputStream inputStream = new FileInputStream(filePath);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
			int m = 0;
			while ((m = bufferedInputStream.read()) != -1) {
				System.out.println("+++++++++ " + m);
				outputStream.write(m);
			}
			if(m == -1){
//				outputStream.write("\n".getBytes());
				outputStream.write("end\n".getBytes());
			}
			bufferedInputStream.close();
			
		}catch (FileNotFoundException e) {
			// TODO: handle exception
			outputStream.write("-- File not found!\n".getBytes());
		}
	}
	
	public void removeFile(String fileName, OutputStream outputStream) throws IOException {
		outputStream.write("-- Remove file!\n".getBytes());
		String filePath = PATH + "/" + fileName;
		File file = new File(filePath);
		if(file.isDirectory()){
			outputStream.write("-- Your command is directory!\n".getBytes());
		}else {
			file.delete();
			outputStream.write("-- Delete succeed!\n".getBytes());
		}
		
	}
	
	public void connect() throws IOException{
		OutputStream outputStream = null;
		InputStream inputStream = null;
		BufferedReader reader = null;
		outputStream = socket.getOutputStream();
		inputStream = socket.getInputStream();
		reader = new BufferedReader(new InputStreamReader(inputStream));
		System.out.println("Server Start .............");
		outputStream.write("Hello\n".getBytes());
		while(true){			
			outputStream.write("-- Input your command.\n".getBytes());
			outputStream.write(" \n".getBytes());
			String line = reader.readLine();
			System.out.println("Command : " + line);
//			String[] command  = line.split(" ");
//			if(command[0].equals("ls")){
//				getFileList(outputStream);
//			}else if(command[0].equals("download")) {
//				downloadFile(command[1], outputStream);
//			}else if(command[0].equals("rm")) {
//				removeFile(command[1], outputStream);
//			}else if(command[0].equals("bye")) {
//				break;
//			}else {
//				outputStream.write(("-- Your command " + command[0] + " error. Pls input again.\n").getBytes());
//			}
			
//			System.out.println("Command : " + line);
//			String[] command  = line.split(" ");
			if(line.startsWith("ls")){
				getFileList(outputStream);
			}else if(line.startsWith("download")) {
				if(checkCommand(outputStream, line)){
					downloadFile(line.split(" ")[1], outputStream);
				}
			}else if(line.startsWith("rm")) {
				if(checkCommand(outputStream, line)){
					removeFile(line.split(" ")[1], outputStream);
				}
			}else if(line.startsWith("bye")) {
				break;
			}else {
				outputStream.write(("-- Your command -" + line + "- error. Pls input again.\n").getBytes());
			}	
		}
		System.out.println("finished ...");
		socket.close();
		outputStream.close();
		inputStream.close();
		reader.close();

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			System.out.println("run init .....");
			this.connect();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
